package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;

import layout.TableLayout;

import com.hexidec.util.Translatrix;

import eventHandling.EventBus;
import eventHandling.EventTyp.Type;

/**
 * The CreateCardDilog is used to create or edit cards. It's like a little WYSIWYG - HTML editor.
 * @author sloth42
 *
 */
public class CreateCardDialog extends JDialog {

	private static final long serialVersionUID = 3871465701317515246L;
	private static final String CLOSE = "close";
	private static final String OK = "ok";
	private EventBus eventBus;
	private JButton btnClose = new JButton();
	private JButton btnOk = new JButton();
	private EditCardPanel editCardPanel = null;
	private JCheckBox checkBoxKeepDialogOpen = new JCheckBox(Translatrix
			.getTranslationString("keepDialogOpen"));

	public CreateCardDialog(EventBus eventBus, MainFrame parent) {
		super(parent);
		this.eventBus = eventBus;

		editCardPanel = new EditCardPanel(null, null);

		initButtons();
		double border = 5;
		double size[][] = {
				{ border, TableLayout.FILL, TableLayout.FILL, border }, // Columns
				{ border, TableLayout.FILL, 20, 30, 30, border } }; // Rows

		this.setLayout(new TableLayout(size));
		// c r c r
		this.add(editCardPanel, "1, 1, 2, 1");
		this.add(checkBoxKeepDialogOpen, "1, 3");
		this.add(btnClose, "1, 4");
		this.add(btnOk, "2, 4");

		this.setSize(400, 400);
		editCardPanel.updateDivider();
	}

	private void initButtons() {

		//
		// Ok
		//
		btnOk.setText(Translatrix.getTranslationString(OK));
		btnOk.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				eventBus.fireEvent(Type.ADD_NEW_CARD);
			}
		});

		//
		// Close
		//
		btnClose.setText(Translatrix.getTranslationString(CLOSE));
		btnClose.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				eventBus.fireEvent(Type.CLOSE_CREATE_CARD_DIALOG);
			}
		});
	}

	public EditCardPanel getEditCardPanel() {
		return editCardPanel;
	}
	
	public boolean keepDialogOpen() {
		return checkBoxKeepDialogOpen.isSelected();
	}

}